package com.homework7;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        //开启一个服务器
        ServerSocket serverSocket = new ServerSocket(9999);
        //接受用户传来的文件
        new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        Socket accept = serverSocket.accept();
                        InputStream inputStream = accept.getInputStream();
                        OutputStream outputStream = accept.getOutputStream();
                        //设置文件存放位置
                        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\admin\\Desktop\\"+System.currentTimeMillis()+".jpg"));
                        int len;
                        byte[] bytes=new byte[8192];
                        while ((len=inputStream.read(bytes))!=-1){
                            fileOutputStream.write(bytes,0,len);
                        }
                        System.out.println("上传完毕........");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
